<?php

class GoodsTag{
	public $tag = array('channel'=>array('block'=>1,'level'=>4));

	public function _channel($attr,$content){
		$cid = $attr['cid'];
		$php =<<<str
			<?php 
				\$category = catched('category');

				foreach (\$category as \$key => \$field):
					\$field['url'] = U('category',array('cid'=>$cid));

			?>
str;
		$php.=$content;
		$php.="<?php endforeach ?>";
		return $php;
	}

	public function _goods($attr,$content){
		// \$cid = explode(',',str_replace(' ', '', \$cid));
		$rows = $attr['rows'];
		
		$cid = $attr['cid'];
		$php =<<<str
			<?php 
				\$goodsDB = M('goods');
				if(!empty(\$cid)){
					\$result = \$goodsDB->where(array('cid'=>\$cid))->limit(\$rows)->all();
					
				}
				foreach (\$result as \$key => \$field):
					\$field['index_pic'] = "__ROOT__/".\$field['index_pic'];
					\$field['url'] = U('content',array('gid'=>\$field['gid']));
				?>


str;
		$php.=$content;
		$php.="<?php  endforeach;?>";
		return $php;
	}

	public function _goodsFlag($attr,$content){
		// \$cid = explode(',',str_replace(' ', '', \$cid));
		$rows = $attr['rows'];
		
		$flag = $attr['flag'];
		$php =<<<str
			<?php 
				\$goodsDB = M('goods');
				if(!empty(\$flag)){
					\$result = \$goodsDB->limit($rows)->where(array('flag'=>\$flag))->all();
					
				}else if(is_array(\$flag)){
					\$flag = implode(',', \$flag);
					\$flag = "($flag)";
					\$sql = 'SELECT * FROM hd_g_goods where flag in \$flag limit 0,$rows';
					\$result = M()->query(\$sql);
					
				}else{
					\$result = \$goodsDB->limit($rows)->all();
				}
					foreach (\$result as \$key => \$field):
						\$field['index_pic'] = "__ROOT__/".\$field['index_pic'];
						\$field['url'] = U('content',array('gid'=>\$field['gid']));
						\$field['num'] = \$key +1;
					?>


str;
		$php.=$content;
		$php.="<?php  endforeach;?>";
		return $php;
	}
}
?>
